<?php
declare (strict_types = 1);
namespace app\index\middleware;
use think\facade\Cache;
class uTokenChecktwoe
{
    public function handle($request, \Closure $next)
    {
        $uToken=$request->header('Token');
        
        if($uToken == ''){
            // return json(['code'=>200,'data'=>['code' =>201, 'msg' => 'uToken不能为空']]);
        }else{
            $uToken='Bearer '.$uToken;
            $uToken = array('Authorization:'.$uToken);
            $url = 'https://xwcmzx.xingtaiwang.net/api/syswechat/curruserinfo';
            

            $userdata = https_requestget($url, null, $uToken);
            $json_userdata = json_decode($userdata);
            $code =  $json_userdata->code;
            if($code == 200){
                $data =  $json_userdata->result;
                $uid =  $data->userId;
                $uuname =  $data->nickName;
                $uopenid =  $data->openId;
                $uunionid =  $data->openId;
                $unickname =  $data->nickName;
                $uheadimgurl =  $data->avatar;


                $test=$request->param();
                $test['uid']=$uid;
                $test['uuname']=$uuname;
                $test['uopenid']=$uopenid;
                $test['uunionid']=$uunionid;
                $test['unickname']=$unickname;
                $test['uheadimgurl']=$uheadimgurl;
                $request->params=$test;
                
            }else{
                return json($json_userdata);
            }
        }
        return $next($request->params);
        
    }

}

